package com.itany.ums.dao.impl;

import com.itany.ums.dao.UserDao;
import com.itany.ums.entity.User;
import com.itany.ums.exception.DataAccessErrorException;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class UserDaoImpl implements UserDao {
    public static final String JDBC_DRIVER_CLASS_NAME = "com.mysql.jdbc.Driver";
    public static final String JDBC_URL = "jdbc:mysql://localhost:3306/ems?useUnicode=true&characterEncoding=utf-8&useSSL=true";
    public static final String JDBC_USERNAME = "root";
    public static final String JDBC_PASSWORD = "";

    @Override
    public void insertUser(User user) throws DataAccessErrorException {
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;

        try {
            Class.forName(JDBC_DRIVER_CLASS_NAME);
            conn= DriverManager.getConnection(JDBC_URL,JDBC_USERNAME,JDBC_PASSWORD);
            String sql="insert into t_user (name,password,phone,address) values(?,?,?,?)";
            ps=conn.prepareStatement(sql);
            ps.setString(1,user.getUsername());
            ps.setString(2,user.getPassword());
            ps.setString(3,user.getPhone());
            ps.setString(4,user.getAddress());
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
            throw new DataAccessErrorException("数据访问错误");
        }
    }

    @Override
    public void selectByUserNameAndPassword(String username, String password) {
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;

        try {
            Class.forName(JDBC_DRIVER_CLASS_NAME);
            conn=DriverManager.getConnection(JDBC_URL,JDBC_USERNAME,JDBC_PASSWORD);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public User findById(int id) {
        return null;
    }

    @Override
    public void delelteById(int id) {

    }

    @Override
    public void updateUser(User user) {

    }
}
